package com.class04.reference;

import com.class04.reference.data.Employee;
import org.junit.jupiter.api.Test;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @ClassDescription:
 * 一、构造器引用
 * 二、数组引用
 *
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/11/28 19:27
 */
public class ConstructorRefTest {
//    构造器引用
    @Test
    public void test1(){
        Supplier<Employee> sup = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println("sup.get() = " + sup.get());
//        构造器引用
//        格式：类名 ::new  调用了类名对应的类中某个确定的构造器
//        具体调用的是类中的哪一个构造器？取决于函数式接口的抽象方法的形参列表！
//        2.数组引用
//        格式：数组名[]::new
        System.out.println("===============================");
        Supplier<Employee> sup1 = Employee::new;
        //调用的是Employee类中空参构造器
        sup1.get();
    }



    @Test//2.构造器引用
    public void test2(){
        Function<Integer,Employee> func = new Function<Integer, Employee>() {
            @Override
            public Employee apply(Integer id) {
                return new Employee(id);
            }
        };
        Employee apply = func.apply(12);
        System.out.println("apply = " + apply);
        System.out.println("===========================");
//        调用的是Employee类中参数是int类类型的构造器
        Function<Integer,Employee> func1 = Employee::new;
        Employee apply1 = func1.apply(23);
        System.out.println("apply1 = " + apply1);
    }
    @Test
    public void test3(){
        BiFunction<Integer,String,Employee> function = new BiFunction<Integer, String, Employee>() {
            @Override
            public Employee apply(Integer id, String name) {
                return new Employee(id,name);
            }
        };
        Employee employee = function.apply(1, "张三");
        System.out.println("employee = " + employee);
        System.out.println("=============");
        BiFunction<Integer,String,Employee> function1 = Employee::new;
        Employee apply = function1.apply(12, "李四");
        System.out.println(apply);

    }
//    数组引用
    @Test
    public void test4(){
        Function<Integer,Employee[]> function = new Function<Integer, Employee[]>() {
            @Override
            public Employee[] apply(Integer length) {
                return new Employee[length];
            }
        };
        System.out.println("function.apply(5) = " + function.apply(5));
        Function<Integer,Employee[]> function1 = Employee[]::new;


    }
}
